﻿
/*
//#region License Information

•————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————•
| Copyright Aptillon, Inc.                                                                                                   |
| This file is part of the CKS:ScriptLibrary project - cksscript.codeplex.com                                                |
| Usage, modification and distribution subject to the license terms available here: http://cksscript.codeplex.com/license    |
|                                                                                                                            |
| Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,                   |
| either express or implied.                                                                                                 |
|                                                                                                                            |
| THIS MESSAGE MAY NOT BE LEGALLY REMOVED                                                                                    |
•————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————• 

//#endregion
*/

//#endregion


var CKS = CKS || {};     //set up top-level namespace "CKS" to avoid polluting the GLOBAL object.  Reuse existing or create new.

//#region Callback support

CKS.Callbacks = CKS.Callbacks || (function () {
    //declare private members here

    function publicMembers() { }

    /**
    * adapted from http://onemarco.com/2008/11/12/callbacks-and-binding-and-callback-arguments-and-references/ 
    * @param {Function} func: the callback function 
    * @param {Object} opts: an object literal with the following properties (all optional): 
    * scope: the object to bind the function to (what the "this" keyword will refer to) 
    * args: an array of arguments to pass to func when called, these will be appended after any arguments passed by the caller 
    * supressArgs: boolean, whether to supress the arguments passed by the caller.  Default is false. */
    publicMembers.Wrapper = function (func, opts) {
        var cb = function () {
            try {
                var args = opts.args ? opts.args : [];
                var scope = opts.scope ? opts.scope : this;
                var fargs = opts.supressArgs === true ? [] : $j.makeArray(arguments);
                if (null != func) {
                    func.apply(scope, fargs.concat(args));
                }
            }
            catch (err) {
                //            var message = "Current Function:{0}. Name={1}.  Message={2}.".format(getCurrentFunctionName(arguments.callee.toString()), err.Name, err.Message);
                //            logError(message);
            }
        }
        return cb;
    }
    return publicMembers;
})();



//#endregion

//#region Console support

CKS.Logger = CKS.Logger || (function () {
    //declare private members here

    function publicMembers() { }
    publicMembers.logToConsole = function (message) {
        if (window.console) {
            window.console.log(message);
        }
    }
    return publicMembers;
})();

//#endregion

//#region ScriptSupport



CKS.ScriptLoader = CKS.ScriptLoader || (function () {
    //declare private members here

    function publicMembers() { }

    ///adapted from: http://www.nczonline.net/blog/2009/06/23/loading-javascript-without-blocking/
    publicMembers.loadScriptWithCallback = function (url, callback) {
        var u = url;
        var c = callback;
        var s = document.createElement('script');
        s.type = 'text/javascript';
        if (s.readyState) {
            s.onreadystatechange = function () {
                if (s.readyState == 'loaded' || s.readyState == 'complete') {
                    s.onreadystatechange = null;
                    c()
                }
            }
        }
        else {
            s.onload = function () {
                c()
            }
        }
        s.src = u;
        document.body.appendChild(s)
    };

    publicMembers.loadScript = function (url) {
        loadScriptWithCallback(url, null);
    };


    publicMembers.jqNoConflict = function () {
        if (typeof (jQuery) != "undefined") {
            $j = jQuery.noConflict();
        }
    };

    publicMembers.loadJq = function (version, debug) {
        loadScriptWithCallback(L_Menu_BaseUrl + "/scripts/jquery-" + version + (debug ? ".debug" : "") + ".js", jqNoConflict);
    }
    return publicMembers;

})();
//#endregion


//#region String Prototype Extensions
String.prototype.format = function () {
    try {
        var formatted = this;
        for (i = 0; i < arguments.length; i++) {
            formatted = formatted.replace("{" + i + "}", arguments[i]);
        }
        return formatted;
    }
    catch (err) {
        return formatted;
    }
};

//#endregion



SP.SOD.notifyScriptLoadedAndExecuteWaitingJobs('cksscriptutilities');
SP.SOD.notifyEventAndExecuteWaitingJobs('cksscriptutilities')
